package cn.rengy.web.framework.datagrid;

import org.apache.commons.lang3.StringUtils;

public class Page{
		protected int pageNo = 1;
		protected int pageSize = 20;
		protected long totalCount = 0;
		protected int totalPage;
		protected String sort = "";//字段名
		protected String order = "";//顺序
		protected String alias="";
		public Page(int pageNo,int pageSize,long totalCount,String order,String sort,String alias){
			this.pageNo=pageNo<1?1:pageNo;
			this.pageSize=pageSize;
			this.totalCount=totalCount;
			this.totalPage=(int)((totalCount / pageSize)+(totalCount % pageSize == 0 ? 0 : 1));
			this.order=order;
			this.sort=sort;
			this.alias=alias;
		}
		public boolean hasNext(){
			return pageNo<totalPage;
		}
		public void next(){
			pageNo++;
		}
		public long getStartRow(){
			return (pageNo-1)*pageSize;
		}
		/**
		 * 拼接order by
		 * 
		 * @param page
		 * @param alias
		 *            别名
		 * @return
		 */
		public String getOrderSql() {
			StringBuilder orderSql = new StringBuilder("");
			if (StringUtils.isBlank(sort))
				return "";
			String alias_="";
			if (StringUtils.isNotBlank(alias)) {
				alias_ = alias + ".";
			}
			orderSql.append(" ORDER BY ");
			if (StringUtils.isNotBlank(order)) {//不为空
				String orders[] = sort.split(",");
				String sorts[] = order.split(",");
				if (orders.length == sorts.length) {// 长度相等
					for (int i = 1; i <= orders.length; i++) {
						orderSql.append(alias_).append(orders[i - 1]).append(" ").append(
								sorts[i - 1]);
						if (i < orders.length) {
							orderSql.append(",");
						}
					}
				} else {// 长度不相等
					for (int i = 1; i <= orders.length; i++) {
						orderSql.append(alias_).append(orders[i - 1]).append(" ").append(
								sorts[0]);
						if (i < orders.length) {
							orderSql.append(",");
						}
					}
				}
			} else {// order 为空
				String orders[] = sort.split(",");
				for (int i = 1; i <= orders.length; i++) {
					orderSql.append(alias_).append(orders[i - 1]);
					if (i < orders.length) {
						orderSql.append(",");
					}
				}
			}
			return orderSql.toString();
		}

		public long getPageNo() {
			return pageNo;
		}

		public void setPageNo(int pageNo) {
			this.pageNo = pageNo;
		}

		public long getPageSize() {
			return pageSize;
		}

		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}

		public long getTotalCount() {
			return totalCount;
		}

		public void setTotalCount(long totalCount) {
			this.totalCount = totalCount;
		}

		public String getOrder() {
			return sort;
		}

		public void setOrder(String order) {
			this.sort = order;
		}

		public String getSort() {
			return order;
		}

		public void setSort(String sort) {
			this.order = sort;
		}

		public String getAlias() {
			return alias;
		}

		public void setAlias(String alias) {
			this.alias = alias;
		}

		public int getTotalPage() {
			return totalPage;
		}

		
		
	}